package com.xhwl.centiotdata.pollingtrigger.roma;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xhwl.centiotdata.pollingtrigger.ScheduleDispatcher;
import com.xhwl.centiotdata.service.impl.RomaDeviceReceiveServiceImpl;
import com.xhwl.centiotdata.service.integrateconfig.IntegrationConfig;
import com.xhwl.centiotdata.suppliersystem.ganwei.GanweiIoTHandler;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class RomaDeviceDispatcher implements ScheduleDispatcher {

    @Autowired
    private RomaDeviceReceiveServiceImpl romaDeviceReceiveService;
    @Autowired
    private GanweiIoTHandler ganweiIoTHandler;

    @Override
    public void doOneHourTask() {
        // 奥特莱斯盥洗室、红外对射设备
        ThreadPoolUtils.executor.execute(() -> {
            log.info("盥洗室任务执行开始========================================================");
            this.syncDevice();

            this.syncToiletRecords();
        });
    }

    @Override
	public void doFiveMinutesTask() {
    	ThreadPoolUtils.executor.execute(() -> {
            this.syncGanweiConsumerMsg();
        });
	}



	/**
     * 同步操作
     */
    public void syncDevice() {
        // 拉取所有集成配置

        List<DeviceProjSupplierCategory> InfraredConfList = IntegrationConfig.integrationConfigList.stream()
                .filter(integrationConfig -> integrationConfig.getProdCategoryId() == CategoryEnum.INFRARED_RADIATION_SYSTEM.getId() && integrationConfig.getProductId() == 21)
                .collect(Collectors.toList());
        romaDeviceReceiveService.getRomaDevice(InfraredConfList.get(0));

        List<DeviceProjSupplierCategory> ToiletConfList = IntegrationConfig.integrationConfigList.stream()
                .filter(integrationConfig -> integrationConfig.getProdCategoryId() == CategoryEnum.TOILET_SYSTEM.getId() && integrationConfig.getProductId() == 21)
                .collect(Collectors.toList());
        romaDeviceReceiveService.getRomaDevice(ToiletConfList.get(0));
    }


    /**
     * 定时拉取盥洗室记录
     */
    public void syncToiletRecords() {
        romaDeviceReceiveService.getToiletDataByTask();
    }
    
    /**
     * 同步敢为iot-roma平台设备
     */
    public void syncGanweiConsumerMsg() {
    	List<DeviceProjSupplierCategory> ganweiRomaConfList = IntegrationConfig.integrationConfigList.stream()
                .filter(config -> Integer.valueOf(11).equals(config.getProdCategoryId()) 
                		&& Integer.valueOf(21).equals(config.getProductId()))
                .collect(Collectors.toList());
    	if(ganweiRomaConfList != null && !ganweiRomaConfList.isEmpty()) {
    		ganweiRomaConfList.forEach(config->{
    			try {
    				ganweiIoTHandler.consumerMsg(config);
				} catch (Exception e) {
					log.error("通过ROMA平台查询敢为IoT平台的设备列表系统异常");
				}
    		});
    		
    	}
	}

}
